//
//  PFPhotosManager.m
//  PrintFamily
//
//  Created by edaihua on 16/2/29.
//  Copyright © 2016年 ___PrintFactory___. All rights reserved.
//

#import "PFPhotosManager.h"
#import "PFPrintManager.h"
#import "PFPhoto.h"

@interface PFPhotosManager()

@property (nonatomic, strong) NSMutableArray *photos; // PFPhotos

@end
@import Photos;
@implementation PFPhotosManager

- (instancetype)initFromLocalStorage {
    if (self = [super init]) {
        // Check selected photos from local storage
        self.photos = [[NSMutableArray alloc] init];
        
    }
    return self;
}

- (void)replaceSelectedPhotoToPrint:(NSMutableArray*)selectedPhotos rectManager:(PFCommonRectManager*)rectManager {
    self.photos = [[NSMutableArray alloc] init];
    for (PHAsset *asset in selectedPhotos) {
        PFPhoto *photo = [[PFPhoto alloc] initWithPHAsset:asset printType:[PFPrintManager getCurrentType] boarder:PhotoBoarder_White rectManager:rectManager];
        NSLog(@"asset pixel size, w:%lu, h:%lu", (unsigned long)asset.pixelWidth, (unsigned long)asset.pixelHeight);
        
        [self.photos addObject:photo];
    }
}

- (void)addSelectedPhotoToPrintDesk:(NSMutableArray*)selectedPhotos rectManager:(PFCommonRectManager*)rectManager {
    for (PHAsset* selectedAsset in selectedPhotos) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"localIdentifier == %@",selectedAsset.localIdentifier];
        NSArray *array = [self.photos filteredArrayUsingPredicate: predicate];
        if (array.count == 0) {
            PFPhoto *photo = [[PFPhoto alloc] initWithPHAsset:selectedAsset printType:[PFPrintManager getCurrentType] boarder:YES rectManager:rectManager];
            [self.photos addObject:photo];
        } else {
            
        }
    }
}

- (NSMutableArray*)getSelectedAssets {
    NSMutableArray *selectedAssets = [[NSMutableArray alloc] init];
    for (PFPhoto *photo in self.photos) {
        [selectedAssets addObject:photo.asset];
    }
    return selectedAssets;
}

- (void)clearSelectedPhotos {
    [self.photos removeAllObjects];
}


- (PFPhoto*)getPhotoAtIndex:(NSUInteger)index {
    PFPhoto *result = nil;
    if (index < self.photos.count) {
        result = [self.photos objectAtIndex:index];
    }
    return result;
}

- (PFPhoto*)getPhotoByLocalIdentifier:(NSString*)identifier {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"localIdentifier == %@", identifier];
    NSArray *array = [self.photos filteredArrayUsingPredicate:predicate];
    PFPhoto *photo = nil;
    if (array.count > 0) {
        photo = array.firstObject;
    }
    return photo;
}

- (PHAsset*)getAssetAtIndex:(NSUInteger)index {
    return [self getPhotoAtIndex:index].asset;
}

- (void)removePhotoAtIndex:(NSUInteger)index {
    if (index < self.photos.count) {
        [self.photos removeObjectAtIndex:index];
    }
}

- (NSUInteger)getPhotoCount {
    return self.photos.count;
}

- (NSMutableArray*)getPhotosMutableCopy {
    return [self.photos copy];
}

@end
